package exam;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
public class yongzhe {

    void unix(){
        Scanner in = new Scanner(System.in);
        String strs;
        LinkedList<String> list;
        int count =1;
        while(true){
            System.out.println("示例:"+count++);
            System.out.print("输入:");
            strs = in.nextLine();
            list = new LinkedList<>();
            for(int i=0;i<strs.length();i++){
                if(strs.charAt(i)!='/'&&strs.charAt(i)!='.'){
                    int start = i;
                    while (i<strs.length()&&strs.charAt(i)!='/'&&strs.charAt(i)!='.') i++;
                    list.addLast(strs.substring(start,i));
                }else if(strs.charAt(i)=='.'){
                    if(i+1<strs.length()&&strs.charAt(i+1)=='.'&&!list.isEmpty()) list.removeLast();// 两个".."返回上一级目录
                }
            }
            StringBuilder sb = new StringBuilder();
            sb.append("/");
            if(list.isEmpty()){
                System.out.print("输出:");
                System.out.println(sb.toString());
            }else{
                while (!list.isEmpty()){
                    sb.append(list.removeFirst());
                    sb.append("/");
                }
                System.out.print("输出:");
                System.out.println(sb.toString().substring(0,sb.length()-1));
            }

        }
    }

    int randomPick(int[] A, int[] W){
        int sum = 0;
        for(int i =0;i<W.length;i++){
            sum += W[i];
            W[i] = sum;
        }
        Random random = new Random();
        int temp = random.nextInt(sum)+1;
        int index = Arrays.binarySearch(W,temp);
        if(index>=0){
            return index;
        }else{
            return -index-1;
        }
    }

    public static void main(String[] args) {
        yongzhe y = new yongzhe();
//        y.unix();
        int[] pick = new int[1];
        for (int i = 0;i<1000;i++){
            int t = y.randomPick(new int[]{1},new int[]{9});
            pick[t]+=1;
        }
        System.out.println("A:[1]   W:[9]");
        System.out.print("出现次数：");
        for (int i:pick){
            System.out.print(i+",");
        }
    }
}
